package com.paradise.phone.adapter;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.paradise.phone.utils.DataBaseHelper;
import com.paradise.phone.OnRecyclerViewItemClickListener;
import com.paradise.phone.R;

import org.linphone.core.CallDirection;
import org.linphone.core.LinphoneCallLog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Sisyphus on 2017/9/21.
 */

public class HistoryAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private LinphoneCallLog[] mCallLogs;

    private OnRecyclerViewItemClickListener listener;

    private SQLiteDatabase db;
    private Cursor cursor;

    public HistoryAdapter(Context mContext) {
        this.mContext = mContext;
        db = DataBaseHelper.instance(mContext).getWritableDatabase();
    }

    private class HistoryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView tvUsername;
        private TextView tvNum;
        private ImageView ivDirection;
        private View vContent;
        private View vHide;
        private TextView tvAdd;
        private TextView tvDelete;
        private TextView tvTime;

        private OnRecyclerViewItemClickListener listener;

        public HistoryViewHolder(View itemView, final OnRecyclerViewItemClickListener listener) {
            super(itemView);
            tvUsername = itemView.findViewById(R.id.tv_remote_display_name);
            tvNum = itemView.findViewById(R.id.tv_remote_num);
            ivDirection = itemView.findViewById(R.id.iv_dir);
            vContent = itemView.findViewById(R.id.ll_content);
            vHide = itemView.findViewById(R.id.ll_hide);
            tvAdd = itemView.findViewById(R.id.tv_add);
            tvDelete = itemView.findViewById(R.id.tv_delete);
            tvTime = itemView.findViewById(R.id.tv_time);

            ViewGroup.LayoutParams params = vContent.getLayoutParams();
            params.width = mContext.getResources().getDisplayMetrics().widthPixels;
            vContent.setLayoutParams(params);

            this.listener = listener;
            tvAdd.setOnClickListener(this);
            tvDelete.setOnClickListener(this);
            vContent.setOnClickListener(this);
            itemView.setOnClickListener(this);
        }

        public TextView getTvUsername() {
            return tvUsername;
        }

        public TextView getTvNum() {
            return tvNum;
        }

        public ImageView getIvDirection() {
            return ivDirection;
        }

        public TextView getTvAdd() {
            return tvAdd;
        }

        public TextView getTvDelete() {
            return tvDelete;
        }

        public TextView getTvTime() {
            return tvTime;
        }

        @Override
        public void onClick(View v) {
            listener.onRecyclerViewItemClickLitener(v, getAdapterPosition());
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new HistoryViewHolder(LayoutInflater.from(mContext)
                .inflate(R.layout.item_call_log, parent, false), listener);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HistoryViewHolder fvh = (HistoryViewHolder) holder;
        fvh.setIsRecyclable(false);
        if (mCallLogs != null && mCallLogs.length > 0) {
            LinphoneCallLog callLog = mCallLogs[position];
            if (callLog.getDirection() == CallDirection.Outgoing) {
                fvh.getTvNum().setText(callLog.getTo().getUserName());
                if (!TextUtils.isEmpty(callLog.getTo().getDisplayName())) {
                    fvh.getTvUsername().setText(callLog.getTo().getDisplayName());
                }else {
                    fvh.getTvUsername().setText("未知联系人");
                }

                fvh.getIvDirection().setImageResource(R.mipmap.outgoing);

                cursor = db.rawQuery("select * from Contact where number = "
                        +callLog.getTo().getUserName(), null);

            } else {
                fvh.getTvNum().setText(callLog.getFrom().getUserName());
                if (!TextUtils.isEmpty(callLog.getFrom().getDisplayName())) {
                    fvh.getTvUsername().setText(callLog.getFrom().getDisplayName());
                }else {
                    fvh.getTvUsername().setText("未知联系人");
                }
                fvh.getIvDirection().setImageResource(R.mipmap.incomming);

                cursor = db.rawQuery("select * from Contact where number = "
                        +callLog.getFrom().getUserName(), null);

            }
            if (cursor.getCount() == 0){
                fvh.getTvAdd().setVisibility(View.VISIBLE);
            }else {
                cursor.moveToNext();
                fvh.getTvUsername().setText(cursor.getString(1));
            }


            fvh.getTvTime().setText(parseTime(callLog.getTimestamp()));
        }
    }

    @Override
    public int getItemCount() {
        return mCallLogs.length;
    }

    public void setData(LinphoneCallLog[] mCallLogs) {
        this.mCallLogs = mCallLogs;
    }

    public void setOnRecyclerViewItemClickLitener(OnRecyclerViewItemClickListener listener){
        this.listener = listener;
    }

    private String parseTime(Long timeStamp){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(timeStamp);
        try {
            Date date = sdf.parse(time);
            if (date.getDay() == new Date().getDay()){
                time = "今天"+time.substring(10);
            }
            if (date.getDay() < new Date().getDay()
                    && date.getDay() > new Date().getDay() - 2){
                time = "昨天"+time.substring(10);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }
}
